package com.shangjinrong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shangjinrong.domain.UserBind;
import com.shangjinrong.domain.UserInfo;
import com.shangjinrong.domain.vo.UserBindVO;
import com.shangjinrong.enums.UserBindEnum;
import com.shangjinrong.excption.Assert;
import com.shangjinrong.hfb.FormHelper;
import com.shangjinrong.hfb.HfbConst;
import com.shangjinrong.hfb.RequestHelper;
import com.shangjinrong.mapper.UserBindMapper;
import com.shangjinrong.mapper.UserInfoMapper;
import com.shangjinrong.result.ResponseEnum;
import com.shangjinrong.service.UserBindService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Naruto
 */
@Service
public class UserBindServiceImpl extends ServiceImpl<UserBindMapper, UserBind> implements UserBindService {


    @Resource
    private UserInfoMapper userInfoMapper;

    @Override
    public String commitBindUser(UserBindVO userBindVo, Long userId) {


        //不同的user_id,相同的身份证，如果存在，则不允许
        QueryWrapper<UserBind> userBindQueryWrapper1 = new QueryWrapper<>();
        userBindQueryWrapper1
                .eq("id_card", userBindVo.getIdCard())
                .ne("user_id", userId);

        UserBind userBind1 = baseMapper.selectOne(userBindQueryWrapper1);
        Assert.isNull(userBind1, ResponseEnum.USER_BIND_IDCARD_EXIST_ERROR);


        //判断用户是否填写过绑定表单
        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();

        userBindQueryWrapper.eq("user_id", userId);

        UserBind bind = baseMapper.selectOne(userBindQueryWrapper);

        if (bind == null) {
            //创建用户绑定记录
            UserBind userBind = new UserBind();
            BeanUtils.copyProperties(userBindVo, userBind);
            userBind.setUserId(userId);
            userBind.setStatus(UserBindEnum.NO_BIND.getStatus().byteValue());

            baseMapper.insert(userBind);
        } else {
            BeanUtils.copyProperties(userBindVo, bind);
            baseMapper.updateById(bind);
        }


        Map<String, Object> paramMap = new HashMap<>();

        paramMap.put("agentId", HfbConst.AGENT_ID);
        paramMap.put("agentUserId", userId);
        paramMap.put("idCard", userBindVo.getIdCard());
        paramMap.put("personalName", userBindVo.getName());
        paramMap.put("bankType", userBindVo.getBankType());
        paramMap.put("bankNo", userBindVo.getBankNo());
        paramMap.put("mobile", userBindVo.getMobile());
        paramMap.put("returnUrl", HfbConst.USERBIND_RETURN_URL);
        paramMap.put("notifyUrl", HfbConst.USERBIND_NOTIFY_URL);

        paramMap.put("timeStamp", RequestHelper.getTimestamp());
        paramMap.put("sign", RequestHelper.getSign(paramMap));


        //动态生成表单字符串
        String formStr = FormHelper.buildForm(HfbConst.USERBIND_URL, paramMap);

        return formStr;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void notify(Map<String, Object> paramMap) {
        String bindCode = paramMap.get("bindCode").toString();
        String agentUserId = paramMap.get("agentUserId").toString();

        //根据userId查询user_bind记录
        QueryWrapper<UserBind> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", agentUserId);

        UserBind userBind = baseMapper.selectOne(wrapper);

        //更新用户绑定表
        userBind.setBindCode(bindCode);
        userBind.setStatus(UserBindEnum.OK_BIND.getStatus().byteValue());
        baseMapper.updateById(userBind);

        //更新用户信息表
        UserInfo userInfo = userInfoMapper.selectById(agentUserId);

        userInfo.setBindCode(bindCode);
        userInfo.setName(userBind.getName());
        userInfo.setIdCard(userBind.getIdCard());
        userInfo.setBindStatus(UserBindEnum.OK_BIND.getStatus().byteValue());

        userInfoMapper.updateById(userInfo);
    }

    @Override
    public String getBindCodeByUserId(Long userId) {
        QueryWrapper<UserBind> userBindQueryWrapper = new QueryWrapper<>();

        userBindQueryWrapper.eq("user_id", userId);

        UserBind userBind = baseMapper.selectOne(userBindQueryWrapper);

        return userBind.getBindCode();
    }

}
